/*   **********************************************************************  **
 **   Copyright notice                                                       **
 **                                                                          **
 **   (c) 2009, Around(J2)ME				                                 **
 **   All rights reserved.                                                   **
 **                                                                          **
 **   This program and the accompanying materials are made available under   **
 **   the terms of the New BSD License which accompanies this    			 **
 **   distribution.											                 **
 **   A copy is found in the textfile LICENSE.txt							 **
 **                                                                          **
 **   This copyright notice MUST APPEAR in all copies of the file!           **
 **                                                                          **
 **   Main developers:                                                       **
 **     Juri Strumpflohner		http://blog.js-development.com	             **
 **		Matthias Braunhofer		http://matthias.jimdo.com                    **
 **                                                                          **
 **  **********************************************************************  */

package org.aroundme.unittests;

import java.io.IOException;
import java.util.ArrayList;

import org.aroundme.model.CategoryName;
import org.aroundme.model.Comment;
import org.aroundme.model.Location;
import org.aroundme.model.Place;
import org.aroundme.model.XMLRequestParser;
import org.aroundme.unitTools.TestFixture;
import org.aroundme.utility.Constants;
import org.jdom.JDOMException;

public class XMLRequestParserTest extends TestFixture {
	private XMLRequestParser xmlHandler;
	
	protected void setUp() throws Exception {
		super.setUp();
		xmlHandler = new XMLRequestParser();
	}

	protected void tearDown() throws Exception {
		super.tearDown();
		xmlHandler = null;
	}

	public void testLoadDocumentModel() throws JDOMException, IOException {
		String xmlString = xmlTestUtils.createXMLCommitString(Constants.COMMIT_PLACE, "fixed", new String[]{CategoryName.HOTELS.toString(), CategoryName.BARS.toString()});
		xmlHandler.loadDocumentModel(xmlString);
		//launches error automatically when exception is thrown, otherwise
		//everything is fine
	}

	public void testGetType() throws Exception {
		String type = Constants.COMMIT_PLACE;
		String xmlString = xmlTestUtils.createXMLCommitString(type, "fixed", new String[]{CategoryName.HOTELS.toString(), CategoryName.BARS.toString()});
		xmlHandler.loadDocumentModel(xmlString);
		
		String returnedType = xmlHandler.getType();
		
		assertEquals("Check if type is correct", type, returnedType);
	}
	
	public void testGetCellid() throws Exception{
		String xmlString = xmlTestUtils.createXMLRetrieveString(Constants.RETRIEVE_PLACE, Constants.PARAM_LOCATION_CELLID, null, null, 0, 9999999);
		xmlHandler.loadDocumentModel(xmlString);
		
		String returnedCellid = xmlHandler.getCellid();
		
		assertEquals("Check if cellid is equal to 1", xmlTestUtils.getDummyLocations().get(1).getCellid(), returnedCellid);
	}
	
	public void testGetUserLoginName() throws Exception{
		String xmlString = xmlTestUtils.createXMLRetrieveString(Constants.RETRIEVE_PLACE, Constants.PARAM_RETRIEVE_PERSONAL, null, null, 0, 9999999);
		xmlHandler.loadDocumentModel(xmlString);
		
		String returnedUser = xmlHandler.getUserLoginName();
		
		assertEquals("return user login", xmlTestUtils.getDummyUsers().get(0).getLoginName(), returnedUser);
	}
	
	public void testGetRequestParameter() throws Exception{
		String param = Constants.RETRIEVE_PLACE;
		String xmlString = xmlTestUtils.createXMLRetrieveString(Constants.RETRIEVE_PLACE, param, null, null, 0, 9999999);
		xmlHandler.loadDocumentModel(xmlString);
		
		String returnedParam = xmlHandler.getRequestParameter();
		
		assertEquals("parameter", param, returnedParam);
	}
	
	public void testGetEncryptedMessage() throws Exception{
		String type = "commit:place";
		String xmlString = xmlTestUtils.createXMLCommitString(type, "fixed",new String[]{CategoryName.HOTELS.toString(), CategoryName.BARS.toString()});
		xmlHandler.loadDocumentModel(xmlString);
		
		String encodedPass = xmlHandler.getEncryptedPassword();
		
		assertNotNull(encodedPass);
	}
	
	public void testGetPlaces() throws Exception{
		String type = "commit:place";
		String xmlString = xmlTestUtils.createXMLCommitString(type, "fixed", new String[]{CategoryName.HOTELS.toString(), CategoryName.BARS.toString()});
		xmlHandler.loadDocumentModel(xmlString);
		
		//retrieve all places
		ArrayList<Place> retrievedPlaces = xmlHandler.getPlaces();
		assertNotNull(retrievedPlaces);
		assertEquals("Should be equally many elements", xmlTestUtils.getDummyPlaces().size(), retrievedPlaces.size());
		for(int i=0; i<xmlTestUtils.getDummyPlaces().size(); i++){
			assertEquals("Name should be equal", xmlTestUtils.getDummyPlaces().get(i).getName(), retrievedPlaces.get(i).getName());
			assertEquals("Address should be equal", xmlTestUtils.getDummyPlaces().get(i).getAddress(), retrievedPlaces.get(i).getAddress());
			assertEquals("user should be equal", xmlTestUtils.getDummyPlaces().get(i).getUser().getLoginName(), retrievedPlaces.get(i).getUser().getLoginName());
			
			assertNotNull("should contain some categories", retrievedPlaces.get(i).getCategories());
			assertTrue("should contain 2 categories", retrievedPlaces.get(i).getCategories().size() == 2);
		}
	}
	
	public void testGetComment() throws Exception{
		xmlTestUtils.saveDummyUsersToDB();
		xmlTestUtils.saveDummyPlacesToDB();
		String type = "commit:placecomment";
		Comment comment = new Comment();
		comment.setComment("hello, test-comment");
		comment.setPlace(xmlTestUtils.getDummyPlaces().get(0));
		comment.setUser(xmlTestUtils.getDummyUsers().get(1));
		String xmlString = xmlTestUtils.createXMLCommitStringComment(type, comment);
		xmlHandler.loadDocumentModel(xmlString);
		
		Comment retrieved = xmlHandler.getComment();
		assertNotNull(retrieved);
		assertNotNull(comment.getPlace());
		assertEquals("place id", comment.getPlace().getId(), retrieved.getPlace().getId());
		assertEquals("comment-content", comment.getComment(), retrieved.getComment());
		assertEquals("user", comment.getUser().getLoginName(), retrieved.getUser().getLoginName());
	}
	
	public void testGetSearchQuery() throws Exception{
		String type = Constants.RETRIEVE_PLACE;
		String searchQuery = "query for search";
		String xmlString = xmlTestUtils.createXMLRetrieveString(type, Constants.PARAM_SEARCH, searchQuery, null, 0, 9999999);
		xmlHandler.loadDocumentModel(xmlString);
		
		String returnedQuery = xmlHandler.getSearchQuery();
		
		assertEquals("Check if query is correct", searchQuery, returnedQuery);
	}
	
	public void testGetRequestDataBeginDataEnd() throws Exception{
		String type = Constants.RETRIEVE_PLACE;
		int begin = 10;
		int end = 20;
		String xmlString = xmlTestUtils.createXMLRetrieveString(type, Constants.PARAM_SEARCH, "", null, begin, end);
		xmlHandler.loadDocumentModel(xmlString);
		
		int beginRetr = xmlHandler.getRequestDataBegin();
		int endRetr = xmlHandler.getDataAmount();
		assertEquals("Begin should be equal", begin, beginRetr);
		assertEquals("End should be equal", end, endRetr);
	}
	
	public void testGetRequestedCategory() throws Exception{
		String category = CategoryName.BARS.toString();
		
		String type = Constants.RETRIEVE_PLACE;
		String xmlString = xmlTestUtils.createXMLRetrieveString(type, Constants.REQPARAM_RETRIEVE_BYCATEGORY, null, category, 0, 9999999);
		xmlHandler.loadDocumentModel(xmlString);
		
		String returnedCategory = xmlHandler.getRequestedCategory();
		
		assertEquals("categories should match", category, returnedCategory);
	}
	
	public void testGetRequestedLocation() throws Exception{
		Location encodedLocation = xmlTestUtils.getDummyLocations().get(1); // this is the one that gets encoded in the dummy XML
		
		String type = Constants.RETRIEVE_PLACE;
		String xmlString = xmlTestUtils.createXMLRetrieveString(type, Constants.REQPARAM_RETRIEVE_BYCATEGORY, null, null, 0, 9999999);
		xmlHandler.loadDocumentModel(xmlString);
		
		Location retrievedLocation = xmlHandler.getRequestedLocation();
		
		assertEquals("lat should be equal", encodedLocation.getLat(), retrievedLocation.getLat());
		assertEquals("long should be equal", encodedLocation.getLon(), retrievedLocation.getLon());
	}
}
